// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::cipher;

@test fn rt_finish() void = {
	const key: [16]u8 = [
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
	];

	const zero: [CT64_EXPKEYSZ]u8 = [0...];

	let block = aes();
	init(&block, key);
	cipher::finish(&block);

	assert(len(block.expkey) == len(zero));
	assert(bytes::equal(zero, block.expkey));
};

@test fn rt_encrypt_128() void = {
	const key: [16]u8 = [
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
	];

	const plain: [16]u8 = [
		0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
		0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
	];

	const cipher: [16]u8 = [
		0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
		0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
	];

	let result: [16]u8 = [0...];

	let block = aes();
	init(&block, key);
	defer cipher::finish(&block);
	cipher::encrypt(&block, result[..], plain[..]);

	assert(bytes::equal(cipher, result));
};

@test fn rt_encrypt_128_multiple_blocks() void = {
	const key: [16]u8 = [
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
	];

	const plain: [_]u8 = [
		0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65,
		0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2,
		0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23,
		0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3,
		0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29,
		0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e,
		0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40,
		0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98
	];

	const cipher: [_]u8 = [
		0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65,
		0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b,
		0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd,
		0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8,
		0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd,
		0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa,
		0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c,
		0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf
	];


	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	// test from 1 to 4 parallel blocks
	for (let i = 0z; i < 4; i += 1) {
		let result: [64]u8 = [0...];

		let sz = (i + 1) * 16;
		cipher::encrypt(&block, result[0..sz], plain[0..sz]);

		assert(bytes::equal(cipher[0..sz], result[0..sz]));
	};
};

@test fn rt_decrypt_128_multiple_blocks() void = {
	const key: [16]u8 = [
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
	];

	const plain: [_]u8 = [
		0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65,
		0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2,
		0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23,
		0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3,
		0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29,
		0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e,
		0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40,
		0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98
	];

	const cipher: [_]u8 = [
		0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65,
		0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b,
		0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd,
		0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8,
		0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd,
		0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa,
		0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c,
		0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf
	];


	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	// test from 1 to 4 parallel blocks
	for (let i = 0z; i < 4; i += 1) {
		let result: [64]u8 = [0...];

		let sz = (i + 1) * 16;
		cipher::decrypt(&block, result[0..sz], cipher[0..sz]);

		assert(bytes::equal(plain[0..sz], result[0..sz]));
	};
};

@test fn rt_decrypt_128() void = {
	const key: []u8 = [
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
	];

	const plain: [16]u8 = [
		0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
		0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
	];

	const cipher: [16]u8 = [
		0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
		0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
	];

	let result: [16]u8 = [0...];

	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	cipher::decrypt(&block, result[..], cipher[..]);

	assert(bytes::equal(plain, result));
};

// fips-197.pdf Appendix C.1
@test fn rt_example_vector1() void = {
	const key: [_]u8 = [
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	];

	const plain: [_]u8 = [
		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
		0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
	];

	const cipher: [_]u8 = [
		0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
		0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a,
	];

	let result: [16]u8 = [0...];

	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	cipher::encrypt(&block, result[..], plain[..]);
	assert(bytes::equal(cipher, result));

	cipher::decrypt(&block, result[..], cipher[..]);
	assert(bytes::equal(plain, result));
};

@test fn rt_example_vector1_in_place() void = {
	const key: [_]u8 = [
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	];

	const plain: [_]u8 = [
		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
		0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
	];

	const cipher: [_]u8 = [
		0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
		0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a,
	];

	let result: [16]u8 = plain;

	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	cipher::encrypt(&block, result[..], result[..]);
	assert(bytes::equal(cipher, result));

	cipher::decrypt(&block, result[..], result[..]);
	assert(bytes::equal(plain, result));
};

// fips-197.pdf Appendix C.2
@test fn rt_example_vector2() void = {
	const key: [_]u8 = [
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	];

	const plain: [_]u8 = [
		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
		0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
	];

	const cipher: [_]u8 = [
		0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
		0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91,
	];

	let result: [16]u8 = [0...];
	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	cipher::encrypt(&block, result[..], plain[..]);
	assert(bytes::equal(cipher, result));

	cipher::decrypt(&block, result[..], cipher[..]);
	assert(bytes::equal(plain, result));
};

// fips-197.pdf Appendix C.3
@test fn rt_example_vector3() void = {
	const key: []u8 = [
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
	];

	const plain: []u8 = [
		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
		0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
	];

	const cipher: []u8 = [
		0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
		0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89,
	];

	let result: [16]u8 = [0...];
	let block = aes();
	init(&block, key[..]);
	defer cipher::finish(&block);

	cipher::encrypt(&block, result[..], plain[..]);
	assert(bytes::equal(cipher, result));

	cipher::decrypt(&block, result[..], cipher[..]);
	assert(bytes::equal(plain, result));
};
